package com.song.tools.generator.config;

import cn.hutool.core.text.StrPool;
import com.song.tools.generator.util.Field;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 枚举值配置：解析<b>状态:1=启用,0=禁用</b>这样格式的枚举值
 *
 * @author song tools
 * @since 2024-06-24
 */
@Data
@Accessors(chain = true)
@Builder(toBuilder = true)
@AllArgsConstructor
@NoArgsConstructor
public class EnumValueConfig {

    /**
     * 枚举中文名称
     */
    private String enumChineseName;

    /**
     * 枚举值列表
     */
    private List<EnumValueProperties> enumValues;

    /**
     * 枚举code的类型
     */
    private String codeType;

    public static final List<String> DEFAULT_NAME_LIST = Arrays.asList(
            "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten",
            "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen", "Twenty");


    public EnumValueConfig(Field enumField, String comment) {
        parseComment(enumField, comment);
    }

    private void parseComment(Field enumField, String comment) {
        // 状态:1=启用,0=禁用
        String[] split = comment.split(StrPool.COLON);
        if (split.length == 2) {
            this.enumChineseName = split[0];
            String[] split1 = split[1].split(StrPool.COMMA);
            if ("Integer".equals(enumField.getPropertyType())) {
                enumValues = new ArrayList<>();
                int index = 0;
                for (String s : split1) {
                    String[] split2 = s.split("=");
                    enumValues.add(new EnumValueProperties(Integer.parseInt(split2[0]), split2[1], DEFAULT_NAME_LIST.get(index).toUpperCase()));
                    index++;
                }
                codeType = "Integer";
            } else if ("String".equals(enumField.getPropertyType())) {
                enumValues = new ArrayList<>();
                int index = 0;
                for (String s : split1) {
                    String[] split2 = s.split("=");
                    enumValues.add(new EnumValueProperties(split2[0], split2[1], DEFAULT_NAME_LIST.get(index).toUpperCase()));
                    index++;
                }
                codeType = "String";
            }
        }
    }

    @Data
    @NoArgsConstructor
    public static class EnumValueProperties {
        private Object code;
        private String desc;
        private String codeName;

        public EnumValueProperties(Object code, String desc, String codeName) {
            this.code = code;
            this.desc = desc;
            this.codeName = codeName;
        }
    }
}
